package com.atwl.school_system.service.serviceImpl;

import com.atwl.school_system.bean.CartInfo;
import com.atwl.school_system.bean.ProInfo;
import com.atwl.school_system.bean.ProPic;
import com.atwl.school_system.mapping.CartDao;
import com.atwl.school_system.mapping.ProDao;
import com.atwl.school_system.mapping.ProPicDao;
import com.atwl.school_system.mapping.StockDao;
import com.atwl.school_system.service.CartService;
import io.netty.util.internal.shaded.org.jctools.util.RangeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicReference;

@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private ProDao proDao;

    @Autowired
    private CartDao cartDao;
    @Autowired
    private ProPicDao proPicDao;
    @Autowired
    private StockDao stockDao;

    @Override
    public void addCart(Integer userId, Integer proId, Integer proNum, String time) {
        String[] timeList = time.split(":");
        Date date = new Date();
        date.setHours(Integer.parseInt(timeList[0]));
        date.setMinutes(Integer.parseInt(timeList[1]));

        ProInfo proInfo = proDao.getProInfoById(proId);
        CartInfo cartInfo = new CartInfo();
        cartInfo.setCreateTime(new Date());
        cartInfo.setUserId(userId);
        cartInfo.setBookingTime(date);
        // TODO 设置属性
        cartInfo.setGoodsAttr("1,蓝色");
        if (proInfo == null) return;
        cartInfo.setGoodsId(proInfo.getId());
        cartInfo.setMarketPrice(proInfo.getProPrice());
        Random random = new Random();
        int suPrice = random.nextInt(5);
        cartInfo.setRealPrice(proInfo.getProPrice() - suPrice > 0 ? proInfo.getProPrice() - suPrice : proInfo.getProPrice());
        cartInfo.setGoodsName(proInfo.getProName());
        cartInfo.setGoodsNum(proNum);
        String picId = proInfo.getProPicId().split(",")[0];
        ProPic proPic = proPicDao.getProPic(Integer.parseInt(picId));
        cartInfo.setThumbUrl(proPic.getPicUrl());
        List<CartInfo> cartInfoList = cartDao.getCarts(userId);
        Double sumPrice = 0.0;
        if (cartInfoList.size() > 0) {
            for (CartInfo info : cartInfoList) {
                sumPrice += info.getRealPrice();
            }
        } else {
            sumPrice = proInfo.getProPrice();
        }
        cartInfo.setUpdateTime(new Date());
        cartInfo.setSumPrice(sumPrice);
        cartInfo.setStockNum(stockDao.getStockNumNoLock(proId));
        cartDao.addCart(cartInfo);
    }

    @Override
    public void updateCart(CartInfo cartInfo) {
        if (cartInfo.getId() == null) return;
        cartInfo.setUpdateTime(new Date());
        cartInfo.setSumPrice(cartInfo.getGoodsNum() * cartInfo.getRealPrice());
        cartDao.updateCart(cartInfo);
    }

    @Override
    public void deleteCart(Integer id) {
        cartDao.deleteCart(id);
    }
}
